<?php

class Controller_Custom extends Controller_TemplateNew
{
    public $_section;
    public $midle;
    public $session;
    
	public function  before() {
            parent::before();
            $this->db = Database::instance();
            $this->section = 'Custom Signs - ';

            $this->_section = 'catalog';

            $this->template->description = $this->settings['description'];
            $this->template->keywords = $this->settings['keywords'];
            $this->template->linkSettings = $this->settings;

            $this->template->header->active='custom';
            $this->template->content = new View('custom/index');
            $this->template->content->downblock = new View('index/downblock');
            $this->template->content->downblock->formIdea = $this->formIdea;
            $this->midle = new View('index/midle');
            
            if (!isset($_SESSION['cart']['custom'])) {
                $_SESSION['cart']['custom'] = array();
            }
            $this->session = $_SESSION['cart'];
            //down Block
            $allImages = $this->mod->getImagesByItem('index');
            $bottomBlock = $this->mod->getItem(95, 'index');
            $this->template->content->downblock->bottomBlock = $bottomBlock;
            if (isset($allImages[$bottomBlock['id']])) {
                foreach ($allImages[$bottomBlock['id']] as $key=>$value) {
                    $allImages[$bottomBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box4');
                }
            }
            if (isset($allImages)) {
                $this->template->content->downblock->allImages = $allImages;
            }
            $bottomBlockCategory = $this->mod->getCategoryByAlias('images-79','images');
            $formConfig = $this->mod->getItemByAlias('9','images');
            $this->template->content->downblock->showForm = ($formConfig['checkbox6']==1) ? true : false;
            $bottomBlockItems = $this->mod->getCategoryItems($bottomBlockCategory['id'],'images','position');
            $tempImg = $this->mod->getImagesByItem('images','position');
            $tempAtt = $this->mod->getAttachmentsByItem('images');
            foreach ($tempImg as $v) {
                $bottomBlockItems[$v[0]['itemId']]['box4'] = $this->mod->imagePath($v[0],'box4');
            }
            foreach ($tempAtt as $v) {
                $bottomBlockItems[$v[0]['itemId']]['attacment'] = $this->mod->attachmentPath($v[0]);
            }
            $this->template->content->downblock->items = $bottomBlockItems;
            //End down block
            //midle Block
            $midle = $this->mod->getItem(202, 'index');
            if (isset($allImages[$midle['id']])) {
                foreach ($allImages[$midle['id']] as $key=>$value) {
                    $temp[$key] = $this->mod->imagePath($value,'box5');
                }
            }
            $this->midle->images = $temp;
            //
            
            
            $signsList = $this->mod->getCategories($this->_section, 'position');
            $this->template->content->signList = $signsList;
            $this->template->title = $this->section.$this->settings['title'];
            $this->addScript('/scripts/jquerytools/jquery.uniform.js');
            $this->addScript('/scripts/custom.js');
            $this->addStyle('/styles/jquerytools/uniform.default.css');
            $this->addStyle('/styles/fonts.css');
            if ($this->neadRenew) {
                $this->template = new View('browser');
            }
	}
    
	public function actionIndex()
	{
            $this->template->content->activeCategoryId = null;
            $this->template->content->signsContent = new View('custom/create');
            $this->template->content->signsContent->midleBlock = $this->midle;
	}
        
        public function actionSave () {
            if (Request::$method == 'POST') {
                $_SESSION['cart']['delivery'] = $_POST['delivery'];
                $_SESSION['cart']['deliveryCost'] = $_POST['deliveryCost'];
                $this->response->redirect('/custom/billing');
            }
            $this->template->content->activeCategoryId = null;
            $this->template->content->signsContent = new View('custom/save');
            $this->template->content->signsContent->session = $this->session['custom'][0];
            $this->template->content->signsContent->midleBlock = $this->midle;
            
        }
        
        public function actionSavesign() {
            $values = array();
            if (Request::$method == 'POST') {
                $values = $_POST;
                $size = ($values['dimension'] == 1) ? array ('18', '24') : array ('24', '36');
                $pdf = new FPDF($values['orientation'], 'in', $size);
                $pdf->AddPage ();
                $values['sessionId'] = session_id();
                foreach ($values as $k => $v) {
                    if (is_array($v)) {
                        preg_match("/\((?P<r>\d+), (?P<g>\d+), (?P<b>\d+)/",$v['color'], $colorArr);
                        preg_match("/(?P<top>\d+)/",$v['top'], $top);
                        preg_match("/(?P<left>\d+)/",$v['left'], $left);
                        preg_match("/(?P<size>\d+)/",$v['size'], $size);
                        if (!in_array(strtolower($v['font-family']), $pdf->CoreFonts)) {
                            $pdf->AddFont(strtolower($v['font-family']),'',  strtolower($v['font-family']).'.php');
                        } else {
                            $v['font-family'] = 'Arial';
                        }
                        $pdf->SetFont(strtolower($v['font-family']),'',(int)($size['size']*72/10));
                        $pdf->SetTextColor($colorArr['r'], $colorArr['g'], $colorArr['b']);
                        $pdf->Text((int)($left['left']/10), (int)($top['top']/10),$v['text']);
                    }
                }
                if (!is_dir('content/custom/'.$values['sessionId'])) {
                    mkdir('content/custom/'.$values['sessionId'],0777);
                }
                $pdf->Output('content/custom/'.$values['sessionId'].'/test.pdf','f');
                $result = array('dir'=>'content/custom/'.$values['sessionId'].'/',
                                        'dimension'=>$values['dimension'],
                                        'orientation'=>$values['orientation']);
                $_SESSION['cart']['custom'] = array(0=>$result);
                $this->autoRender = false;
                $this->response->setHeader('Content-Type', 'application/json');
                $this->response->body = json_encode(array('result'=>true));
            }
        }
        
        public function actionShow ($itemId) {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            
            
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('signs/show');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            
        }
        public function actionBilling () {
            $this->template->content->activeCategoryId = null;
//            print_r ($this->session);
            //billingForm
            $billingForm = new Form('billing');
            $billingForm->template = 'billingForm';
            $billingForm->text('firstName',null,'First Name')->required('true');
            $billingForm->text('secondName',null,'Second Name')->rule('required');
            $billingForm->text('adressLine1',null,'Adress Line1', 'new')->rule('required');
            $billingForm->text('adressLine2',null,'Adress Line2')->rule('required');
            $billingForm->text('city',null,'City')->rule('required');
            $billingForm->select('state', array_merge(array('' => 'State'), Text::$states), 'State')->rule('required')->bunched(true);
            $billingForm->text('zip',null,'Zip Code')->rule('required')->bunched(true);
            $billingForm->text('email',null,'Email')->rule('required')->rule('email');
            $billingForm->text('email',null,'Email')->rule('required')->rule('email');
            
            //billingForm
            $shippingForm = new Form('shipping');
            $shippingForm->template = 'billingForm';
            $shippingForm->text('firstName',null,'First Name')->required('true');
            $shippingForm->text('secondName',null,'Second Name')->rule('required');
            $shippingForm->text('adressLine1',null,'Adress Line1', 'new')->rule('required');
            $shippingForm->text('adressLine2',null,'Adress Line2')->rule('required');
            $shippingForm->text('city',null,'City')->rule('required');
            $shippingForm->select('state', array_merge(array('' => 'State'), Text::$states), 'State')->rule('required')->bunched(true);
            $shippingForm->text('zip',null,'Zip Code')->rule('required')->bunched(true);
            $shippingForm->text('email',null,'Email')->rule('required')->rule('email');
            $shippingForm->text('email',null,'Email')->rule('required')->rule('email');
            if (isset($this->session['shipping'])) {
                $shippingForm->loadValues($this->session['shipping']);
            }
            if (isset($this->session['billing'])) {
                $billingForm->loadValues($this->session['billing']);
                $billingForm->hidden('notReset', 1);
            }
            
            if ($this->session['custom'][0]['orientation'] == 'P') {
                $this->template->content->signsContent = new View('custom/billingvertical');
            } else {
                $this->template->content->signsContent = new View('custom/billinghorizontal');
            }
            
            if (Request::$method == 'POST' && Request::$isAjax) {
//                print_r ($_POST);
//                exit;
                if ($billingForm->validate()) {
                    $valueBilling = $billingForm->getValues();
                    $valueShipping = $shippingForm->getValues();
                    foreach ($valueBilling as $k=>$v) {
                        $_SESSION['cart']['billing'][$k] = $valueBilling[$k];
                    }
                    $data = array('response'=>true);
                } 
                if ($shippingForm->validate()) {
                    $valueShipping = $shippingForm->getValues();
                    foreach ($valueShipping as $k=>$v) {
                        $_SESSION['cart']['shipping'][$k] = $valueShipping[$k];
                    }
                    $data = array('response'=>true);
                } else {
                    $data = array('response'=>false);
                    if ($billingForm->getErrors()) {
                        $data['billing'] = $billingForm->getErrors();
                    }
                    if ($shippingForm->getErrors()) {
                        $data['shipping'] = $shippingForm->getErrors();
                    }
                }
                $this->autoRender = false;
                $this->response->setHeader('Content-Type', 'application/json');
                $this->response->body = json_encode($data);
                return;
            }

            $this->template->content->signsContent->session = $this->session['custom'][0];
            $this->template->content->signsContent->delivery = $this->session;
            $this->template->content->signsContent->midleBlock = $this->midle;
            $this->template->content->signsContent->billingForm = $billingForm;
            $this->template->content->signsContent->shippingForm = $shippingForm;
        }
        
        
        public function actionFinalize ($itemId) {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('signs/finalize');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->buyItem = array($item);

        }
        public function actionThank ($itemId) {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('signs/thank');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->buyItem = array($item);

        }
}